package dshop.action.admin;

import java.util.List;

import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.Record;

import appbase.jfinal.ext.ApiBaseController;
import appbase.jfinal.ext.RecordKit;
import appbase.utils.ListUtil;
import dshop.interceptor.ApiAuthInterceptor;
import dshop.interceptor.ApiReadBodyParamInterceptor;
import dshop.model.permission.PermissionDict;
import dshop.model.permission.PermissionUser;

/**
 * 权限分配
 */
@Before({ApiReadBodyParamInterceptor.class, ApiAuthInterceptor.class})
public class PermissionAction extends ApiBaseController{

	public void permissionEdit()
	{
		int userId = getParaToInt("userId");
		
		List<Record> permList = PermissionDict.listBy(PermissionDict.LEVEL_ONE);
		
		List<Integer> permIds = RecordKit.getToList(permList, "id", Integer.class);
		List<Record> childPermList = PermissionDict.childListOf(permIds);
		PermissionUser.setStatusTo(childPermList, "id", userId);
		
		RecordKit.mergeRecordsTo(permList, childPermList, "id", "pId", "childList");
		setAttr("permList", permList);
		
		renderSuccess();
	}
	
	public void permissionModify()
	{
		int userId = getParaToInt("userId");
		
		String permIdStr = getPara("permIdStr");
		List<Integer> permIdList = ListUtil.toIntegerList(permIdStr, ",");
		
		PermissionUser.save(userId, permIdList);
		
		renderSuccess("权限设置成功");
	}
	
}
